<?php
namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Statement extends Base {

//    收货地址
//    省
    public function sheng()
    {
        $res = DB::name('Areas')->where('area_parent_id',0)->select();
        if($res)
        {
            $data['info'] = '省';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//      下一级
    public function shi()
    {
       $where['area_parent_id']=input('id');
       $res= DB::name('Areas')->where($where)->select();
        if($res)
        {
            $data['info'] = '市';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

    //添加收货地址
    public function addAddress(){
        $member=$this->member;
        $arr['user_id']=$member['user_id'];
        $arr['consignee']=input('name');
        $arr['province']=input('areaId1');
        $arr['city']=input('areaId2');
        $arr['district']=input('areaId3');
        $arr['mobile']=input('phone');
        $arr['address']=input('address');
        $arr['is_default']=input('moren');
        if($arr['user_id'] == null  || $arr['consignee'] == null  || $arr['mobile'] == null  || $arr['address'] == null  || $arr['is_default'] == null  || $arr['province'] == null  || $arr['city'] == null  || $arr['district'] == null)
        {
            $this->error('请完善信息');
        }
        $id=DB::name('User_address')->insertGetId($arr);
        if($arr['is_default']==1){
            $update['is_default']='0';
            DB::name('User_address')->where('user_id',$arr['user_id'])->where('address_id','neq',$id)->update($update);
        }
        if($id){
            $data['info']='添加成功';
            $data['status']='1';
            $data['data']=array();
            return json($data);
        }else{
            $data['info']='参数错误';
            $data['status']='-1';
            $data['data']=array();
            return json($data);
        }
    }

//    删除收货地址
    public function delAddress()
    {
        $address_id = input('id');
        $res = DB::name('User_address')->where('address_id',$address_id)->delete();
        if($res)
        {
            $data['info'] = '删除成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '删除失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//    修改收货地址
    public function editAddress()
    {
       $id = input('id');
       $res = DB::name('User_address')->where('address_id',$id)->find();
       $res['sheng']= DB::name('Areas')->where('id',$res['province'])->value('area_name');
       $res['shi'] = DB::name('Areas')->where('id',$res['city'])->value('area_name');
       $res['qu'] = DB::name('Areas')->where('id',$res['district'])->value('area_name');
       if($res)
       {
           $data['info'] = '获取成功';
           $data['status'] = '1';
           $data['data'] = $res;
           return json($data);
       }else{
           $data['info'] = '获取失败';
           $data['status'] = '-1';
           $data['data'] = array();
           return json($data);
       }
    }
    public function doeditAddress()
    {
        $id =input('id');
        $member=$this->member;
        $arr['user_id']=$member['user_id'];
        $arr['consignee']=input('name');
        $arr['province']=input('areaId1');
        $arr['city']=input('areaId2');
        $arr['district']=input('areaId3');
        $arr['mobile']=input('phone');
        $arr['address']=input('address');
        $arr['is_default']=input('moren');
        if($arr['user_id'] == null  || $arr['consignee'] == null  || $arr['mobile'] == null  || $arr['address'] == null  || $arr['is_default'] == null  || $arr['province'] == null  || $arr['city'] == null  || $arr['district'] == null)
        {
            $this->error('请完善信息');
        }
        $list = DB::name('User_address')->where('address_id',$id)->update($arr);
        if($arr['is_default']==1){
            $update['is_default']='0';
            DB::name('User_address')->where('user_id',$arr['user_id'])->where('address_id','neq',$id)->update($update);
        }
        if($arr)
        {
            $data['info'] = '修改成功';
            $data['status'] = '1';
            $data['data'] = $arr;
            return json($data);
        }else{
            $data['info'] = '修改失败';
            $data['status'] = '-2';
            $data['data'] = array();
            return json($data);
        }
    }
//      查看我的收货地址
    public function cha()
    {
        $member =$this->member;
        $res = DB::name('User_address')->where('user_id',$member['user_id'])->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['province'] = DB::name('Areas')->where('id',$v['province'])->value('area_name');
            $res[$k]['city'] = DB::name('Areas')->where('id',$v['city'])->value('area_name');
            $res[$k]['district'] = DB::name('Areas')->where('id',$v['district'])->value('area_name');
        }
        if($res)
        {
            $data['info'] = '获取成功';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '获取失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

}